/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

package com.gitee.minimalismstyle.fresh.flowable.api.feign

import org.springframework.web.bind.annotation.*
import com.gitee.minimalismstyle.fresh.common.core.domain.R
import com.gitee.minimalismstyle.fresh.flowable.api.annotation.FlowableClient
import com.gitee.minimalismstyle.fresh.flowable.api.config.FlowableFeignConfig
import com.gitee.minimalismstyle.fresh.flowable.api.feign.fallback.DeploymentApiFallback
import com.gitee.minimalismstyle.fresh.flowable.api.feign.model.DataResponse
import com.gitee.minimalismstyle.fresh.flowable.api.feign.model.DeploymentResourceResponse
import com.gitee.minimalismstyle.fresh.flowable.api.feign.model.DeploymentResponse
import org.springframework.http.MediaType
import org.springframework.web.multipart.MultipartFile

/**
 * Deployment
 * @author maoxiaodong
 */
@FlowableClient(configuration = [FlowableFeignConfig::class], fallback = DeploymentApiFallback::class)
interface DeploymentApi {


    /**
     * List Deployments
     */
    @GetMapping("/repository/deployments")
    fun listDeployments(
        @RequestParam(name = "name", required = false) name: String?,
        @RequestParam(name = "nameLike", required = false) nameLike: String?,
        @RequestParam(name = "category", required = false) category: String?,
        @RequestParam(name = "categoryNotEquals", required = false) categoryNotEquals: String?,
        @RequestParam(name = "parentDeploymentId", required = false) parentDeploymentId: String?,
        @RequestParam(name = "parentDeploymentIdLike", required = false) parentDeploymentIdLike: String?,
        @RequestParam(name = "tenantId", required = false) tenantId: String?,
        @RequestParam(name = "tenantIdLike", required = false) tenantIdLike: String?,
        @RequestParam(name = "withoutTenantId", required = false) withoutTenantId: Boolean?,
        @RequestParam(name = "sort", required = false) sort: String?
    ): R<DataResponse<DeploymentResponse>>

    /**
     * The request body should contain data of type multipart/form-data. There should be exactly one file in the request, any additional files will be ignored. The deployment name is the name of the file-field passed in. If multiple resources need to be deployed in a single deployment, compress the resources in a zip and make sure the file-name ends with .bar or .zip.

    An additional parameter (form-field) can be passed in the request body with name tenantId. The value of this field will be used as the id of the tenant this deployment is done in.
     */
    @PostMapping(value=["/repository/deployments"], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
    fun uploadDeployment(
        @RequestParam(name = "deploymentKey", required = false) deploymentKey: String?,
        @RequestParam(name = "deploymentName", required = false) deploymentName: String?,
        @RequestParam(name = "tenantId", required = false) tenantId: String?,
        @RequestParam(name = "file", required = true) file: MultipartFile
    ): R<DeploymentResponse>

    /**
     * Get a deployment
     */
    @GetMapping("/repository/deployments/{deploymentId}")
    fun getDeployment(
        @PathVariable("deploymentId") deploymentId: String
    ): R<DeploymentResponse>

    /**
     * Delete a deployment
     */
    @DeleteMapping("/repository/deployments/{deploymentId}")
    fun deleteDeployment(
        @PathVariable("deploymentId") deploymentId: String,
        @RequestParam(name = "cascade", required = false) cascade: Boolean?
    ): R<Any>

    /**
     * The response body will contain the binary resource-content for the requested resource. The response content-type will be the same as the type returned in the resources mimeType property. Also, a content-disposition header is set, allowing browsers to download the file instead of displaying it.
     */
    @GetMapping("/repository/deployments/{deploymentId}/resourcedata/{resourceName}")
    fun getDeploymentResourceData(
        @PathVariable("deploymentId") deploymentId: String,
        @PathVariable("resourceName") resourceName: String
    ): R<Any>

    /**
     * The dataUrl property in the resulting JSON for a single resource contains the actual URL to use for retrieving the binary resource.
     */
    @GetMapping("/repository/deployments/{deploymentId}/resources")
    fun listDeploymentResources(
        @PathVariable("deploymentId") deploymentId: String
    ): R<Any>

    /**
     * Replace ** by ResourceId
     */
    @GetMapping("/repository/deployments/{deploymentId}/resources/**")
    fun getDeploymentResource(
        @PathVariable("deploymentId") deploymentId: String
    ): R<DeploymentResourceResponse>
}